package org.jeecg.glr.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.glr.entity.GlrFaultAlerm;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.glr.entity.GlrGateway;
import org.jeecg.glr.service.IGlrFaultAlermService;
import org.jeecg.glr.service.IGlrGatewayService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 故障告警
 * @Author: jeecg-boot
 * @Date:   2020-03-09
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrFaultAlerm")
@Slf4j
public class GlrFaultAlermController extends JeecgController<GlrFaultAlerm, IGlrFaultAlermService> {
	@Autowired
	private IGlrFaultAlermService glrFaultAlermService;
	 @Autowired
	 private IGlrGatewayService glrGatewayService;

	 @Autowired
	 private ISysDictService dictService;
	/**
	 * 分页列表查询
	 *
	 * @param glrFaultAlerm
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrFaultAlerm glrFaultAlerm,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GlrFaultAlerm> queryWrapper = QueryGenerator.initQueryWrapperCode(glrFaultAlerm, req.getParameterMap());
		Page<GlrFaultAlerm> page = new Page<GlrFaultAlerm>(pageNo, pageSize);
		IPage<GlrFaultAlerm> pageList = glrFaultAlermService.page(page, queryWrapper);

		List<GlrFaultAlerm> list = pageList.getRecords();
		for (GlrFaultAlerm faultAlerm : list)
		{
			String message="";
			String str=faultAlerm.getContent();

			GlrGateway glrGateway=glrGatewayService.getGlrGatewayByCode(faultAlerm.getDeviceId());
			if(glrGateway != null)
			{
				faultAlerm.setDeviceName(glrGateway.getGatewayName()+"-"+faultAlerm.getLigCode()+"号灯路");
			}
			faultAlerm.setContentMessage(message);
			if(StringUtil.isNotEmpty(faultAlerm.getDeviceType())){
				if(faultAlerm.getDeviceType().equals(GLRConstant.GLR_LIGHT_POLE_DEVICE_03)){
					if(StringUtil.isNotEmpty(str)){
						if(str.equals("FF")){
							message=dictService.queryDictTextByKey("t_b_light_fault",str);
						}else{
							String strs[]=str.split(",");
							int j=0;
							for(int i=strs.length-1;i>=0;i--){
								if(!strs[i].equals("0")){
									if(StringUtil.isNotEmpty(message)){
										message=message+","+dictService.queryDictTextByKey("t_b_light_fault",String.valueOf(j));
									}else{
										message=dictService.queryDictTextByKey("t_b_light_fault",String.valueOf(j));
									}
								}
								j++;
							}
						}
					}
					faultAlerm.setContentMessage(message);
				}else if(faultAlerm.getDeviceType().equals("7")){
					faultAlerm.setContentMessage(faultAlerm.getContent());
					faultAlerm.setDeviceName(glrGateway.getGatewayName());
				}
			}
		}


		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param glrFaultAlerm
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrFaultAlerm glrFaultAlerm) {
		glrFaultAlermService.save(glrFaultAlerm);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param glrFaultAlerm
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrFaultAlerm glrFaultAlerm) {
		glrFaultAlermService.updateById(glrFaultAlerm);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		glrFaultAlermService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.glrFaultAlermService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrFaultAlerm glrFaultAlerm = glrFaultAlermService.getById(id);
		if(glrFaultAlerm==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrFaultAlerm);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrFaultAlerm
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrFaultAlerm glrFaultAlerm) {
        return super.exportXls(request, glrFaultAlerm, GlrFaultAlerm.class, "故障告警");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrFaultAlerm.class);
    }

	 /**
	  * 通过id查询
	  *
	  * @return
	  */
	 @PostMapping(value = "/processing")
	 public Result<?> processing(@RequestBody GlrFaultAlerm glrFaultAlerm) {
		 glrFaultAlermService.processing(Arrays.asList(glrFaultAlerm.getIds().split(",")));
		 return Result.ok("操作成功");
	 }

}
